package com.zfjt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.BrabchBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.entity.UserApplyBean;
import com.zfjt.entity.UserBean;
import com.zfjt.packnum.PackNum00001;
import com.zfjt.service.BrabchService;
import com.zfjt.service.UserApplyService;
import com.zfjt.service.UserService;


/**
 * 用户注册接口
 * @author xk
 * @version 1.0
 * @date 2017年3月25日
 */
@Controller
public class UserApplyController {
	
	private ReturnBean mReturn;
	
	private Map<String, String> fhdata;
	
	@Autowired
	private UserApplyService UserApply;
	
	@Autowired
	private UserService User;
	
	@Autowired
	private BrabchService Brabch;
	 
	@RequestMapping(value="00001")
	@ResponseBody
	public ReturnBean Global(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00001 no00001 = JSON.parseObject(data,PackNum00001.class);
		
		List<UserBean> mAllUser = User.selectAll();
		List<UserApplyBean> mAllUserApple = UserApply.selectAll();
		//插入数据库的对象
		UserApplyBean mUserApply = new UserApplyBean();
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00001");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{//校验码正确
			int is = 0;
			for (int i = 0; i < mAllUser.size(); i++) {
				UserBean dg = mAllUser.get(i);
				if (dg.getAccount().equals(no00001.getPhone())) {
					is = 1;
				}
			}
			//申请表中以后这个账号
			for (int i = 0; i < mAllUserApple.size(); i++) {
				UserApplyBean dg = mAllUserApple.get(i);
				if (dg.getAccount().equals(no00001.getPhone())) {
					is = 2;
				}
			}
			if(is == 2){//申请表中有这条参数，不能注册，返回错误；当前账号已申请注册
				mReturn.setCode("002");
				mReturn.setPortnum("00001");
				fhdata.put("error", "USERAPPLY_ERROR");
				mReturn.setData(JSON.toJSONString(fhdata));
			}else if(is == 1){//用户表中有这条参数，不能注册，返回错误；当前账号已存在
				mReturn.setCode("002");
				mReturn.setPortnum("00001");
				fhdata.put("error", "USER_ERROR");
				mReturn.setData(JSON.toJSONString(fhdata));
			}else if(is == 0){
				if(no00001.getPassword().equals(no00001.getPassword_again())){//两次密码一致	
					BrabchBean brabch = Brabch.selectByPrimaryKey(no00001.getBranch());
					List<UserApplyBean> mdata = UserApply.selectAll();
					int num = 1;
					mUserApply.setPassword(no00001.getPassword());
					if (mdata.size() == 0) {
						num = 1;
					}else{
						num = mdata.get(mdata.size() - 1).getId() + 1;
					}
					short in = (short)num;
					mUserApply.setId(in);
					mUserApply.setPhone(no00001.getPhone());
					mUserApply.setType("");
					mUserApply.setBranch(no00001.getBranch());
					mUserApply.setAccount(no00001.getPhone());
					mUserApply.setName(no00001.getName());
					mUserApply.setBranchname(brabch.getBrabchName());
					mUserApply.setPhoneNum(no00001.getPhone_num());
					UserApply.insert(mUserApply);
					fhdata.put("cg","cg");
					mReturn.setCode("000");
					mReturn.setPortnum("00001");
					mReturn.setData(JSON.toJSONString(fhdata));
					}else{//两次密码不一致
						mReturn.setCode("002");
						mReturn.setPortnum("00001");
						fhdata.put("error", "PASSWORD_ERROR");
						mReturn.setData(JSON.toJSONString(fhdata));
					}
			}				
		} 
		return mReturn;
	}
}
